package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {

    @DubboReference
    private AdminService adminService;

    // 渲染权限信息视图的处理器
    @GetMapping("/auth")
    public String auth() {
        return "frame/auth";
    }

    /*渲染首页的控制器*/
    @GetMapping("/")
    public String index(ModelMap modelMap){
        // 谁登录?  如何获得谁登录了?
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user =(User)authentication.getPrincipal();
        String username = user.getUsername();
        Admin admin = adminService.getByUsername(username);
        // 暂时写死成一个id
        /*long adminId=1;
        Admin admin = adminService.getById(adminId);*/
        List<Permission> permissionList=adminService.findMenuPermissionByAdminId(admin.getId());
        // 当前登录的用户
        // 查询当前 用户的所有的权限 放入请求域
        modelMap.addAttribute("admin",admin);
        modelMap.addAttribute("permissionList",permissionList);

        return "frame/index";
    }


    /*渲染main页面的控制器*/
    @RequestMapping("/main")
    public String main(){
        return "frame/main";
    }

    /*渲染登录视图*/
    @RequestMapping("/login")
    public String toLogin(){
        return "frame/login";
    }

}
